package com.zxy.ider.ehome.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class FileUtils {

    public static String getMIMEType(File file)
    {
        String type="*/*";
        String fName=file.getName();
        //获取后缀名前的分隔符"."在fName中的位置。
        int dotIndex = fName.lastIndexOf(".");
        if(dotIndex < 0){
            return type;
        }
    /* 获取文件的后缀名 */
        String end=fName.substring(dotIndex,fName.length()).toLowerCase();
        if(end=="")return type;
        //在MIME和文件类型的匹配表中找到对应的MIME类型。
        for(int i=0;i<MIME_MapTable.length;i++){
            if(end.equals(MIME_MapTable[i][0]))
                type = MIME_MapTable[i][1];
        }
        return type;
    }

    //建立一个MIME类型与文件后缀名的匹配表
    public static String[][] MIME_MapTable={
            //{后缀名，    MIME类型}
            {".3gp",    "video/3gpp"},
            {".apk",    "application/vnd.android.package-archive"},
            {".asf",    "video/x-ms-asf"},
            {".avi",    "video/x-msvideo"},
            {".bin",    "application/octet-stream"},
            {".bmp",      "image/bmp"},
            {".c",        "text/plain"},
            {".class",    "application/octet-stream"},
            {".conf",    "text/plain"},
            {".cpp",    "text/plain"},
            {".doc",    "application/msword"},
            {".exe",    "application/octet-stream"},
            {".gif",    "image/gif"},
            {".gtar",    "application/x-gtar"},
            {".gz",        "application/x-gzip"},
            {".h",        "text/plain"},
            {".htm",    "text/html"},
            {".html",    "text/html"},
            {".jar",    "application/java-archive"},
            {".java",    "text/plain"},
            {".jpeg",    "image/jpeg"},
            {".jpg",    "image/jpeg"},
            {".js",        "application/x-javascript"},
            {".log",    "text/plain"},
            {".m3u",    "audio/x-mpegurl"},
            {".m4a",    "audio/mp4a-latm"},
            {".m4b",    "audio/mp4a-latm"},
            {".m4p",    "audio/mp4a-latm"},
            {".m4u",    "video/vnd.mpegurl"},
            {".m4v",    "video/x-m4v"},
            {".mov",    "video/quicktime"},
            {".mp2",    "audio/x-mpeg"},
            {".mp3",    "audio/x-mpeg"},
            {".mp4",    "video/mp4"},
            {".mpc",    "application/vnd.mpohun.certificate"},
            {".mpe",    "video/mpeg"},
            {".mpeg",    "video/mpeg"},
            {".mpg",    "video/mpeg"},
            {".mpg4",    "video/mp4"},
            {".mpga",    "audio/mpeg"},
            {".msg",    "application/vnd.ms-outlook"},
            {".ogg",    "audio/ogg"},
            {".pdf",    "application/pdf"},
            {".png",    "image/png"},
            {".pps",    "application/vnd.ms-powerpoint"},
            {".ppt",    "application/vnd.ms-powerpoint"},
            {".prop",    "text/plain"},
            {".rar",    "application/x-rar-compressed"},
            {".rc",        "text/plain"},
            {".rmvb",    "video/x-pn-realaudio"},
            {".rtf",    "application/rtf"},
            {".sh",        "text/plain"},
            {".tar",    "application/x-tar"},
            {".tgz",    "application/x-compressed"},
            {".txt",    "text/plain"},
            {".wav",    "audio/x-wav"},
            {".wma",    "audio/x-ms-wma"},
            {".wmv",    "audio/x-ms-wmv"},
            {".wps",    "application/vnd.ms-works"},
            //{".rar",    "application/rar"},
            {".xml",    "text/plain"},
            {".z",        "application/x-compress"},
            {".zip",    "application/zip"},
            {"",        "*/*"}
    };
    public static Bitmap fitSizePic(File f){
        Bitmap resizeBmp = null;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        //数字越大读出的图片占用的heap越小 不然总是溢出
        if(f.length()<20480){         //0-20k
            opts.inSampleSize = 1;
        }else if(f.length()<51200){   //20-50k
            opts.inSampleSize = 2;
        }else if(f.length()<307200){  //50-300k
            opts.inSampleSize = 4;
        }else if(f.length()<819200){  //300-800k
            opts.inSampleSize = 6;
        }else if(f.length()<1048576){ //800-1024k
            opts.inSampleSize = 8;
        }else{
            opts.inSampleSize = 10;
        }
        resizeBmp = BitmapFactory.decodeFile(f.getPath(),opts);
        return resizeBmp;
    }

    /**
     * 文件大小描述
     * @param f
     * @return
     */
    public static String  fileSizeMsg(File f){
        int sub_index = 0;
        String  show = "";
        if(f.isFile()){
            long length = f.length();
            if(length>=1073741824){
                sub_index = (String.valueOf((float)length/1073741824)).indexOf(".");
                show = ((float)length/1073741824+"000").substring(0,sub_index+3)+"GB";
            }else if(length>=1048576){
                sub_index = (String.valueOf((float)length/1048576)).indexOf(".");
                show =((float)length/1048576+"000").substring(0,sub_index+3)+"MB";
            }else if(length>=1024){
                sub_index = (String.valueOf((float)length/1024)).indexOf(".");
                show = ((float)length/1024+"000").substring(0,sub_index+3)+"KB";
            }else if(length<1024){
                show = String.valueOf(length)+"B";
            }
        }
        return show;
    }

    /**
     * 获取文件创建时间
     *
     *
     */
    public static void getCreateTime(String filePath){
        String strTime = null;
        try {
            Process p = Runtime.getRuntime().exec("cmd /C dir "
                    + filePath
                    + "/tc" );
            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            while((line = br.readLine()) != null){
                if(line.endsWith(".txt")){
                    strTime = line.substring(0,17);
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("创建时间    " + strTime);
        //输出：创建时间   2009-08-17  10:21
    }
    /**
     * 文件最后修改的时间
     */
    public static String getModifiedTime_2(String filePath){
        File f = new File(filePath);
        Calendar cal = Calendar.getInstance();
        long time = f.lastModified();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cal.setTimeInMillis(time);
        System.out.println("修改时间[] " + formatter.format(cal.getTime()));
        String times=formatter.format(cal.getTime());
        return times;
    }
    /**
     * 校验输入的文件夹名称是否合法
     * @param newName
     * @return
     */
    public static boolean checkDirPath(String newName){
        boolean ret = false;
        if(newName.indexOf("\\")==-1){
            ret = true;
        }
        return ret;
    }

    /**
     * 校验输入的文件名称是否合法
     * @param newName
     * @return
     */
    public static boolean checkFilePath(String newName){
        boolean ret = false;
        if(newName.indexOf("\\")==-1){
            ret = true;
        }
        return ret;
    }
}
